package com.yaodian100.ec.comment.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.entity.SimplePager;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.core.common.service.DomainService;
import com.yaodian100.core.common.utils.DateUtil;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.ec.product.entity.OrderComment;
import com.yaodian100.ec.product.entity.OrderComment.COMMENT_STATUS;
import com.yaodian100.ec.product.entity.OrderCommentLog;
import com.yaodian100.ec.product.entity.Product;
import com.yaodian100.ec.product.entity.ProductComment;
import com.yaodian100.ec.product.service.OrderCommentService;
import com.yaodian100.ec.product.service.ProductService;

/**
 * @author jinwei.lin
 * 
 */
public class CommentAction extends ActionSupport implements Preparable {

	private static final long serialVersionUID = 178887580319281383L;
	/** logger */
	private final Logger logger = LoggerFactory.getLogger(CommentAction.class);

	@Resource
	private OrderCommentService orderCommentService;
	@Resource
	private ProductService productService;
	private DomainService<Menu> menuService; // 與 setter

	private Menu approveStMenu; // 與 getter, setter
	private Menu hideYnStMenu; // 與 getter, setter
	private Menu sizeMenu; // 與 getter, setter
	private Menu logtypeMenu; // 與 getter, setter
	private SimplePager pager;
	private SimplePager commentProductPager;

	// 查詢條件
	private String orderIdParam;
	private String memberIdParam;
	private String loginIdParam;
	private String commentDtStParam;
	private String commentDtEdParam;
	private String commentStParam;
	private String hideStParam;
	private String productIdParam;

	private String hide;
	private String commentStatus;
	private String failReason;
	private String cmid;
	private String updatemsg;
	private String productId;
	private String commentTp;
	private String star;

	private ProductComment productComment;
	private Product product;
	private List<OrderComment> commentProductList;
	private final String nowStep = "5";

	private String hidemsg;
	private List<OrderComment> commentList;
	private OrderComment orderComment;
	private List<OrderCommentLog> commentLogList;

	@Override
	public void prepare() throws Exception {
		clearErrorsAndMessages();
		if (org.apache.commons.lang.StringUtils.isBlank(commentDtStParam)) {
			commentDtStParam = DateUtil.convertDateToString("yyyy-MM-dd", DateUtil.getTodayAddDays(-60).getTime());
		}
		if (org.apache.commons.lang.StringUtils.isBlank(commentDtEdParam)) {
			commentDtEdParam = DateUtil.convertDateToString("yyyy-MM-dd", DateUtil.getToday().getTime());
		}

		if (approveStMenu == null) {
			approveStMenu = menuService.get("comment.status");
		}
		if (hideYnStMenu == null) {
			hideYnStMenu = menuService.get("comment.hideSt");
		}
		if (sizeMenu == null) {
			sizeMenu = menuService.get("comment.size");
		}
		if (logtypeMenu == null) {
			logtypeMenu = menuService.get("comment.logtype");
		}
	}

	public String index() throws Exception {
		updatemsg = "";
		return "comment.list";
	}

	public String search() throws Exception {
		CommonCriteria crit = new CommonCriteria();
		if (StringUtils.isNotBlank(orderIdParam)) {
			crit.addEq("orderId", orderIdParam);
		}
		if (StringUtils.isNotBlank(memberIdParam)) {
			crit.addEq("memberId", memberIdParam);
		}
		if (StringUtils.isNotBlank(loginIdParam)) {
			crit.addEq("loginId", loginIdParam);
		}
		if (StringUtils.isNotBlank(productIdParam)) {
			crit.addEq("productId", productIdParam);
		}
		if (StringUtils.isNotBlank(commentStParam)) {
			crit.addEq("commentStatus", commentStParam);
		}
		if (StringUtils.isNotBlank(hideStParam)) {
			crit.addEq("hideYn", hideStParam);
		}
		if (StringUtils.isBlank(commentDtStParam)) {
			commentDtStParam = DateUtil.convertDateToString("yyyy-MM-dd", DateUtil.getTodayAddDays(-60).getTime());
		}
		if (StringUtils.isBlank(commentDtEdParam)) {
			commentDtEdParam = DateUtil.convertDateToString("yyyy-MM-dd", DateUtil.getToday().getTime());
		}
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		crit.addGe("createDate", format.parse(commentDtStParam + " 00:00:00"));
		crit.addLe("createDate", format.parse(commentDtEdParam + " 23:59:59"));

		if (pager == null) {
			pager = new SimplePager();
		}
		int entitySize = orderCommentService.getListSize(crit).intValue();
		pager.setTotalSize(entitySize);
		commentList = orderCommentService.getList(pager.getCurrentPage() * pager.getPageRecord(), pager.getPageRecord(),
				crit, new String[] { "createDate desc" });
		updatemsg = "";
		return "comment.list";
	}

	public String loadComment() throws Exception {
		if (StringUtils.isBlank(cmid)) {
			throw new CoreException("评论编号未传入");
		}
		orderComment = orderCommentService.get(cmid);
		if (orderComment == null) {
			if (commentLogList != null && !commentLogList.isEmpty()) {
				commentLogList.clear();
			}
			throw new CoreException("查无此评论");
		}
		commentLogList = orderCommentService.getOrderCommnetLogByCmid(cmid);
		return "comment.detail";
	}

	public String remarks() throws Exception {
		if (StringUtils.isBlank(cmid)) {
			throw new CoreException("评论编号未传入");
		}
		if (StringUtils.isBlank(failReason)) {
			throw new CoreException("请输入说明");
		}
		orderCommentService.addCommentLog(cmid, "10", failReason);
		updatemsg = "更新完成";
		return "updateReason";
	}

	public String product() throws Exception {
		if (StringUtils.isBlank(productId)) {
			throw new CoreException("商品编号未传入");
		}
		product = productService.get(productId);
		productComment = orderCommentService.getProductCommentByProductId(productId);
		if (productComment == null) {
			productComment = new ProductComment();
		}
		star = "0";
		if (productComment.getAvgScore().movePointRight(1).intValue() % 10 > 0) {
			star = "5";
		}
		CommonCriteria crit = new CommonCriteria();

		crit.addEq("productId", productId);
		crit.addEq("hideYn", "N");
		crit.addEq("commentStatus", COMMENT_STATUS.APPROVE.getValue());
		if (StringUtils.isNotBlank(commentTp)) {
			if ("Good".equals(commentTp)) {
				ArrayList<String> seList = new ArrayList<String>();
				seList.add("5");
				seList.add("4");
				crit.addIn("score", seList);
			}
			if ("Mid".equals(commentTp)) {
				ArrayList<String> seList = new ArrayList<String>();
				seList.add("3");
				seList.add("2");
				crit.addIn("score", seList);
			}
			if ("Low".equals(commentTp)) {
				crit.addEq("score", "1");
			}
		}
		if (commentProductPager == null) {
			commentProductPager = new SimplePager();
		}
		int entitySize = orderCommentService.getListSize(crit).intValue();
		commentProductPager.setTotalSize(entitySize);
		commentProductList = orderCommentService.getList(
				commentProductPager.getCurrentPage() * commentProductPager.getPageRecord(),
				commentProductPager.getPageRecord(), crit, new String[] { "createDate desc" });
		updatemsg = "";
		return "comment.product";
	}

	// 核可 / 不核可
	public String approve() throws Exception {
		if (StringUtils.isBlank(cmid)) {
			throw new CoreException("评论编号未传入");
		}
		if (OrderComment.COMMENT_STATUS.valueOf(commentStatus) == null) {
			throw new CoreException("状态错误");
		}
		if (OrderComment.COMMENT_STATUS.valueOf(commentStatus).equals(OrderComment.COMMENT_STATUS.FAIL)
				&& StringUtils.isBlank(failReason)) {
			throw new CoreException("评论讯息未输入");
		}
		orderCommentService.approveOrderComment(cmid, commentStatus, failReason, "comment");
		updatemsg = "审核完成";
		return "updateReason";
	}

	// 隐藏 / 不隐藏
	public String commentProcess() throws Exception {
		if (StringUtils.isBlank(cmid)) {
			throw new CoreException("评论编号未传入");
		}
		if (StringUtils.isBlank(hide)) {
			throw new CoreException("隐藏参数错误");
		}
		if (orderCommentService.hideOrderComment(cmid, hide)) {
			hidemsg = "true";
		} else {
			hidemsg = "false";
		}
		return "jsonHidemsg";
	}

	// 重算
	public String calcProductComment() throws Exception {
		if (StringUtils.isBlank(productId)) {
			throw new CoreException("商品编号未传入");
		}
		if (orderCommentService.calcProductComment(productId)) {
			hidemsg = "true";
		} else {
			hidemsg = "false";
		}
		return "jsonHidemsg";
	}

	public OrderCommentService getOrderCommentService() {
		return orderCommentService;
	}

	public void setOrderCommentService(OrderCommentService orderCommentService) {
		orderCommentService = orderCommentService;
	}

	public DomainService<Menu> getMenuService() {
		return menuService;
	}

	public void setMenuService(DomainService<Menu> menuService) {
		this.menuService = menuService;
	}

	public Menu getApproveStMenu() {
		return approveStMenu;
	}

	public void setApproveStMenu(Menu approveStMenu) {
		this.approveStMenu = approveStMenu;
	}

	public Menu getHideYnStMenu() {
		return hideYnStMenu;
	}

	public void setHideYnStMenu(Menu hideYnStMenu) {
		this.hideYnStMenu = hideYnStMenu;
	}

	public SimplePager getPager() {
		return pager;
	}

	public void setPager(SimplePager pager) {
		this.pager = pager;
	}

	public String getOrderIdParam() {
		return orderIdParam;
	}

	public void setOrderIdParam(String orderIdParam) {
		this.orderIdParam = orderIdParam;
	}

	public String getMemberIdParam() {
		return memberIdParam;
	}

	public void setMemberIdParam(String memberIdParam) {
		this.memberIdParam = memberIdParam;
	}

	public String getLoginIdParam() {
		return loginIdParam;
	}

	public void setLoginIdParam(String loginIdParam) {
		this.loginIdParam = loginIdParam;
	}

	public String getCommentDtStParam() {
		return commentDtStParam;
	}

	public void setCommentDtStParam(String commentDtStParam) {
		this.commentDtStParam = commentDtStParam;
	}

	public String getCommentDtEdParam() {
		return commentDtEdParam;
	}

	public void setCommentDtEdParam(String commentDtEdParam) {
		this.commentDtEdParam = commentDtEdParam;
	}

	public String getCommentStParam() {
		return commentStParam;
	}

	public void setCommentStParam(String commentStParam) {
		this.commentStParam = commentStParam;
	}

	public String getHideStParam() {
		return hideStParam;
	}

	public void setHideStParam(String hideStParam) {
		this.hideStParam = hideStParam;
	}

	public String getProductIdParam() {
		return productIdParam;
	}

	public void setProductIdParam(String productIdParam) {
		this.productIdParam = productIdParam;
	}

	public String getCmid() {
		return cmid;
	}

	public void setCmid(String cmid) {
		this.cmid = cmid;
	}

	public List<OrderComment> getCommentList() {
		return commentList;
	}

	public void setCommentList(List<OrderComment> commentList) {
		this.commentList = commentList;
	}

	public Menu getSizeMenu() {
		return sizeMenu;
	}

	public void setSizeMenu(Menu sizeMenu) {
		this.sizeMenu = sizeMenu;
	}

	public OrderComment getOrderComment() {
		return orderComment;
	}

	public void setOrderComment(OrderComment orderComment) {
		this.orderComment = orderComment;
	}

	public List<OrderCommentLog> getCommentLogList() {
		return commentLogList;
	}

	public void setCommentLogList(List<OrderCommentLog> commentLogList) {
		this.commentLogList = commentLogList;
	}

	public String getHide() {
		return hide;
	}

	public void setHide(String hide) {
		this.hide = hide;
	}

	public String getCommentStatus() {
		return commentStatus;
	}

	public void setCommentStatus(String commentStatus) {
		this.commentStatus = commentStatus;
	}

	public String getFailReason() {
		return failReason;
	}

	public void setFailReason(String failReason) {
		this.failReason = failReason;
	}

	public String getHidemsg() {
		return hidemsg;
	}

	public void setHidemsg(String hidemsg) {
		this.hidemsg = hidemsg;
	}

	public Menu getLogtypeMenu() {
		return logtypeMenu;
	}

	public void setLogtypeMenu(Menu logtypeMenu) {
		this.logtypeMenu = logtypeMenu;
	}

	public String getUpdatemsg() {
		return updatemsg;
	}

	public void setUpdatemsg(String updatemsg) {
		this.updatemsg = updatemsg;
	}

	public ProductService getProductService() {
		return productService;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

	public SimplePager getCommentProductPager() {
		return commentProductPager;
	}

	public void setCommentProductPager(SimplePager commentProductPager) {
		this.commentProductPager = commentProductPager;
	}

	public String getProductId() {
		return productId;
	}

	public void setProductId(String productId) {
		this.productId = productId;
	}

	public String getCommentTp() {
		return commentTp;
	}

	public void setCommentTp(String commentTp) {
		this.commentTp = commentTp;
	}

	public ProductComment getProductComment() {
		return productComment;
	}

	public void setProductComment(ProductComment productComment) {
		this.productComment = productComment;
	}

	public Product getProduct() {
		return product;
	}

	public void setProduct(Product product) {
		this.product = product;
	}

	public List<OrderComment> getCommentProductList() {
		return commentProductList;
	}

	public void setCommentProductList(List<OrderComment> commentProductList) {
		this.commentProductList = commentProductList;
	}

	public String getNowStep() {
		return nowStep;
	}

	public String getStar() {
		return star;
	}

	public void setStar(String star) {
		this.star = star;
	}

}